package com.yzh.wanandroid.utils

import android.app.Activity
import android.content.Context
import android.widget.ImageView
import androidx.fragment.app.Fragment
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import com.bumptech.glide.request.RequestOptions


fun load(
    fragment: Fragment,
    imageView: ImageView,
    url: String,
    errorRes: Int,
    placeholderRes: Int,
    transformation: BitmapTransformation,
    skipMemory: Boolean
) {
    val requestOptions = RequestOptions()
        .placeholder(placeholderRes)
        .error(errorRes)
        .transform(transformation)
        .skipMemoryCache(skipMemory)
    Glide.with(fragment).load(url)
        .apply(requestOptions)
        .into(imageView)
}

fun load(
    fragment: Fragment,
    imageView: ImageView,
    url: String,
    errorRes: Int,
    placeholderRes: Int,
    skipMemory: Boolean
) {
    try {
        val requestOptions = RequestOptions()
            .placeholder(placeholderRes)
            .error(errorRes)
            .skipMemoryCache(skipMemory)
        Glide.with(fragment).load(url)
            .apply(requestOptions)
            .into(imageView)
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

fun load(context: Context, imageView: ImageView, url: String) {
    Glide.with(context).load(url).into(imageView)
}

fun load(
    context: Context,
    imageView: ImageView,
    url: String,
    errorRes: Int,
    skipMemory: Boolean
) {
    val requestOptions = RequestOptions()
        .error(errorRes)
        .skipMemoryCache(skipMemory)
    Glide.with(context).load(url)
        .apply(requestOptions)
        .into(imageView)
}

fun loadOnDiskSubscriptionCache(
    fragment: Fragment,
    imageView: ImageView,
    width: Int,
    height: Int,
    url: String,
    errorRes: Int,
    placeholderRes: Int,
    transformation: BitmapTransformation,
    skipMemory: Boolean
) {
    val requestOptions = RequestOptions()
        .placeholder(placeholderRes)
        .error(errorRes)
        .override(width, height)
        .transform(transformation)
        .diskCacheStrategy(DiskCacheStrategy.NONE)
        .skipMemoryCache(skipMemory)
    Glide.with(fragment).load(url)
        .apply(requestOptions)
        .into(imageView)
}


fun loadOnDiskCache(context: Context, imageView: ImageView, url: String, skipMemory: Boolean) {
    val requestOptions = RequestOptions()
        .fitCenter()
        .diskCacheStrategy(DiskCacheStrategy.ALL)
        .skipMemoryCache(skipMemory)
    Glide.with(context).load(url)
        .apply(requestOptions)
        .into(imageView)
}

fun loadOnDiskCache(
    fragment: Fragment, imageView: ImageView,
    url: String, errorRes: Int, placeholderRes: Int,
    transformation: BitmapTransformation?, width: Int, height: Int, skipMemory: Boolean
) {
    val requestOptions = RequestOptions()
        .placeholder(placeholderRes)
        .error(errorRes)
        .override(width, height)
        .fitCenter()
        .transform(transformation)
        .diskCacheStrategy(DiskCacheStrategy.ALL)
        .skipMemoryCache(skipMemory)
    Glide.with(fragment).load(url)
        .apply(requestOptions)
        .into(imageView)
}

fun loadFitCenter(activity: Activity, url: String, imageView: ImageView) {
    val requestOptions = RequestOptions().fitCenter()
    Glide.with(activity).load(url).apply(requestOptions).into(imageView)
}